package com.example.demo.model.goods.goods.service.impl;

import com.example.demo.Results.BasePage;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Results.ResultClass;
import com.example.demo.model.goods.goods.entity.ShopGoodsType;
import com.example.demo.model.goods.goods.mapper.ShopGoodsTypeDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.model.goods.goods.service.ShopGoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author STAHYW
 * @since 2021-10-02
 */
@Service
public class ShopGoodsTypeServiceImpl extends ServiceImpl<ShopGoodsTypeDao, ShopGoodsType> implements ShopGoodsTypeService {

    @Autowired
    ShopGoodsTypeDao shopGoodsTypeDao;



    @Override
    public ModelAndView goodsTypeList(Integer pageNo, Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView();
        Page page = BasePage.getPage(pageNo, pageSize);
        IPage iPage = shopGoodsTypeDao.selectPage(page, null);
        modelAndView.addObject("iPage", iPage);

        modelAndView.setViewName("/goods/goodsTypeList");
        return modelAndView;
    }

    @Override
    public ResultClass goodsTypeDel(@RequestParam("id") Integer id) {
        shopGoodsTypeDao.deleteById(id);
        return ResultClass.ok("操作成功！");
    }

    @Override
    public ModelAndView goodsTypeEdit(Integer id) {
        ModelAndView modelAndView = new ModelAndView();

        if (id != null) {
            ShopGoodsType shopGoodsType = shopGoodsTypeDao.selectById(id);
            modelAndView.addObject("shopGoodsType", shopGoodsType);
        }

        modelAndView.setViewName("/goods/goodsTypeEdit");


        return modelAndView;

    }

    @Override
    public ResultClass goodsTypeEditDo(ShopGoodsType shopGoodsType) {
        if (shopGoodsType.getId() != null  ) {
            shopGoodsTypeDao.updateById(shopGoodsType);
        } else {
            shopGoodsTypeDao.insert(shopGoodsType);
        }
        return ResultClass.ok("操作成功！");
    }

}
